# -*- coding: utf-8 -*-
"""
Date: 2018-12-01
Author: Likejian
"""
from pyshare.bll.validators import *
from pyshare.utils.time_utils import getCT, addTimes, isPassDay
from pyshare.const.enums.harem import PalaceState
from pyshare.dal.gameconfig.game_env import get_config_env_set


class PalaceBuildingsMixin(object):
    __slots__ = ()
    palace_id = NotImplemented
    popularity = NotImplemented
    palace_lv = NotImplemented
    palace_state = NotImplemented
    built_complete_time = NotImplemented
    last_change_time = NotImplemented
    concubine_id = NotImplemented
    draft_left = NotImplemented
    last_draft_time = NotImplemented
    palace_costume_list = NotImplemented
    current_palace_costume = NotImplemented

    def popularity_increase(self, val):
        """ 增加人气 """
        self.popularity += val
        self.save()

    def level_up(self, consume_popularity, next_lv=None):
        """ 升级 """
        if next_lv:
            lv = next_lv
        else:
            lv = self.palace_lv + 1
        self.palace_lv = lv            
        self.popularity -= consume_popularity

        if self.popularity < 0:
            self.popularity = 0
        self.save()

    def start_construction(self, minutes):
        """ 开始建造 """
        self.palace_state = PalaceState.UnderConstruction
        self.built_complete_time = addTimes(
            getCT(), 
            minutes=minutes
        )
        self.save()

    def finish_construction(self):
        """ 完成建造 """
        self.palace_state = PalaceState.FinishConstruction 
        self.save()

    def concubine_change(self, concubine):
        """ 更换妃子 """
        from pyshare.dal.gameconfig.query import getProfile
        if concubine:
            profile=getProfile('TcPalaceBuilt', PalaceId=self.palace_id)
            validation([
                # 该妃子有足够的亲密等级
                EnoughConcubineIntimateLvValidator(
                    concubine, 
                    profile.ResideIntimateLv
                ),
            ])
            self.concubine_id = concubine.cfg_id
            self.last_change_time = getCT()
            self.save()
    
    def concubine_clear(self):
        """ 清空妃子 """
        self.concubine_id = 0
        self.save()

    def update_change_time(self):
        self.last_change_time = getCT()
        self.save()

    def is_reside(self):
        """ 是否入住了 """
        return self.concubine_id > 0
    
    def reside_is_cooldown(self, cd_time):
        """ 是否在冷却中 """
        return self.last_change_time + cd_time <= getCT()

    def pull_down(self):
        """拆除建筑"""
        self.delete()

    def palace_costume_add(self, costume_id):
        """ 宫殿新增皮肤 """
        costume = self.palace_costume_list
        costume.append(costume_id)
        self.palace_costume_list = costume
        self.current_palace_costume = costume_id
        self.save()

    def change_current_costume(self, costume_id):
        """更换宫殿皮肤"""
        self.current_palace_costume = costume_id
        self.save()


class PalaceRoleMixin(object):
    __slots__ = ()
    prosperity = NotImplemented
    harem = NotImplemented
    current_dinner_dress = NotImplemented
    last_change_time = NotImplemented

    def prosperity_increase(self, val):
        """ 繁荣度增加 """
        self.prosperity += val
        self.save()

    def get_harem_num(self, harem_id):
        """ 获取某个秀女数量 """
        return self.harem.get(str(harem_id), 0)

    def harem_assign(self, harem_id, num=1):
        """ 分配秀女 """
        harem = self.harem
        harem[str(harem_id)] -= num
        self.harem = harem
        self.save()

    def change_dinner_dress(self, dress_id):
        """ 改变宴请台装扮 """
        self.current_dinner_dress = dress_id
        self.save()

    def dinner_dress_add(self, dress_id):
        """ 宴请台新增装扮 """
        dress = self.dinner_dress_list
        dress.append(dress_id)
        self.dinner_dress_list = dress
        self.save()

    def update_change_time(self):
        self.last_change_time = getCT()
        self.save()

    def reside_is_cooldown(self, cd_time):
        """ 是否在冷却中 """
        return self.last_change_time + cd_time <= getCT()   

    def draft_times_update(self):
        """ 更新选秀次数 """
        if isPassDay(self.last_draft_time):
            self.draft_left = get_config_env_set('PalaceDefaultDraftNum')
            self.save()


class PartialPalaceMixin(object):
    __slots__ = ()
    palace_id = NotImplemented
    concubine_id = NotImplemented
    chamber_id = NotImplemented
    last_change_time = NotImplemented

    def concubine_change(self, concubine):
        """ 更换妃子 """
        from pyshare.dal.gameconfig.query import getProfile
        if concubine:
            profile = getProfile('TcPalaceBuilt', PalaceId=self.palace_id)
            validation([
                # 该妃子有足够的亲密等级
                EnoughConcubineIntimateLvValidator(
                    concubine,
                    profile.ResideIntimateLv
                ),
            ])
            self.concubine_id = concubine.cfg_id
            self.last_change_time = getCT()
            self.save()

    def concubine_clear(self):
        """ 清空妃子 """
        self.concubine_id = 0
        self.save()

    def is_reside(self):
        """ 是否入住了 """
        return self.concubine_id > 0

    def reside_is_cooldown(self, cd_time):
        """ 是否在冷却中 """
        return self.last_change_time + cd_time <= getCT()
